import requests
from lxml import etree
from lxml.etree import _Element


def get_page(url: str):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
    }

    resp = requests.get(url=url, headers=headers)
    html = resp.text
    return html


def parse_page(content: str):
    return etree.HTML(content)


def parse_title(tree: _Element):
    return tree.xpath('/html/head/title/text()')[0]


def parse_table_head(tree: _Element):
    items = tree.xpath('//*[@id="top20"]/thead/tr/th/text()')
    return items


def parse_second_table_head(tree: _Element):
    items = tree.xpath('//*[@id="otherPL"]/thead/tr/th/text()')
    return items


def parse_table_body(tree: _Element):
    rows = tree.xpath('//*[@id="top20"]/tbody/tr')
    columns = list()
    for row in rows:
        # 获取元素的值
        items = row.xpath("./td/text()")
        # print(items)
        # 获取元素属性的值
        src = row.xpath("./td[3]/img/@src")
        # print(src)
        change = ''
        if len(src) == 0:
            pass
        else:
            # print(type(src[0]))
            # print(str(src[0]))
            string = str(src[0])
            if string.find("up.png") != -1:
                change = '↑'
            if string.find("upup.png") != -1:
                change = '↑↑'
            if string.find("down.png") != -1:
                change = '↓'
            if string.find("downdown.png") != -1:
                change = '↓↓'
        # print(change)

        items.insert(2, change)
        # print(items)
        columns.append(items)
    return columns


def parse_second_table_body(tree: _Element):
    rows = tree.xpath('//*[@id="otherPL"]/tbody/tr')
    columns = list()
    for row in rows:
        # 获取元素的值
        items = row.xpath("./td/text()")
        columns.append(items)
    return columns


def write_file(file: str, table_head: list, table_body: list):
    file_name = file + ".csv"
    with open(file=file_name, mode="w", encoding="utf-8") as f:
        f.write(",".join(table_head))
        f.write("\n")

        for row in table_body:
            f.write(",".join(row))
            f.write("\n")


if __name__ == '__main__':
    url = "https://www.tiobe.com/tiobe-index/"
    # step 1 下载页面内容
    html = get_page(url=url)
    # print(html)

    # step 2 对整个网页进行解析
    tree = parse_page(html)
    # print(type(tree))
    title = parse_title(tree)
    print(title)

    # table_head = parse_table_head(tree)
    # print(table_head)
    # table_body = parse_table_body(tree)
    #
    # write_file(title, table_head, table_body)

    second_table_head = parse_second_table_head(tree)
    print(second_table_head)

    second_table_body = parse_second_table_body(tree)
    for row in second_table_body:
        print(row)
